package com.coscoshipping.rams.demo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @description 代码生成类
 * @author zhaoyin.yang
 * @date 2022/12/1 11:08
 */
public class AutoGenerator {

    @Test
    public void codeGenerator() throws IOException {
        // 数据源配置
        DataSourceConfig.Builder DATA_SOURCE_CONFIG
                = new DataSourceConfig.Builder("jdbc:mysql://192.168.3.142:3306/znhw-test?createDatabaseIfNotExist=true&useSSL=false",
                "znhw-test",
                "1LW9NdDp");

        // 填充字段信息
        List<IFill> tableFills = new ArrayList<>();
        IFill uuid = new Column("UUID", FieldFill.INSERT);
        IFill tenantUuid = new Column("TENANT_UUID", FieldFill.INSERT);
        IFill customerUuid = new Column("CUSTOMER_UUID", FieldFill.INSERT);
        IFill userUuid = new Column("USER_UUID", FieldFill.INSERT);
        IFill createdBy = new Column("CREATED_BY", FieldFill.INSERT);
        IFill createdTime = new Column("CREATED_TIME", FieldFill.INSERT);
        IFill updatedBy = new Column("UPDATED_BY", FieldFill.INSERT_UPDATE);
        IFill updatedTime = new Column("UPDATED_TIME", FieldFill.INSERT_UPDATE);
        tableFills.add(uuid);
        tableFills.add(tenantUuid);
        tableFills.add(customerUuid);
        tableFills.add(userUuid);
        tableFills.add(createdBy);
        tableFills.add(createdTime);
        tableFills.add(updatedBy);
        tableFills.add(updatedTime);

        // 需要做代码生成的表名
        String[] tableList={"ticket_sales_statistics"};

        // 项目绝对路径
        File directory = new File("");
        String projectPath = directory.getCanonicalPath();

        // 项目的包名
        String packageName=AutoGenerator.class.getPackage().getName();

        // 代码生成
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig(builder -> {
                    builder
                            .outputDir(projectPath+"\\src\\main\\java")
                            .author("zhaoyin.yang")
                            .enableSpringdoc()
                            .dateType(DateType.TIME_PACK)
                            .commentDate("yyyy-MM-dd");
                })
                // 包配置
                .packageConfig(builder -> {
                    builder
                            .parent(packageName)
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller");
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder
                            .enableCapitalMode()
                            .enableSkipView()
                            .disableSqlFilter()
                            .addInclude(tableList)
                            .controllerBuilder()
                            .enableRestStyle()
                            //.enableFileOverride()
                            .serviceBuilder()
                            //.enableFileOverride()
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .enableFileOverride()
                            .entityBuilder()
                            .enableLombok()
                            .enableFileOverride()
                            .enableColumnConstant()
                            .addTableFills(tableFills)
                            .idType(IdType.AUTO)
                            .enableTableFieldAnnotation()
                    ;
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                // 执行
                .execute();
    }
}